package org.xxf.tdengine.demo.taos;

import cn.hutool.core.date.DateUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.xxf.tdengine.demo.entity.TaosLp;

import java.sql.*;

public class TaosQueryDemo {

    public static void main(String[] args) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:TAOS-WS://192.168.52.132:6041/soundanaly?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8");
        config.setUsername("root");
        config.setPassword("Qwe123@awa");
        // maximum-pool-size
        config.setMinimumIdle(10); // minimum number of idle connection
        config.setMaximumPoolSize(10); // maximum number of connection in the pool
        config.setConnectionTimeout(30000); // maximum wait milliseconds for get connection from pool
        config.setMaxLifetime(0); // maximum life time for each connection
        config.setIdleTimeout(0); // max idle time for recycle idle connection
        config.setConnectionTestQuery("SELECT SERVER_VERSION()"); // validation query
        HikariDataSource dataSource = new HikariDataSource(config);

        Thread thread = new Thread(new Runnable() {
            public void run() {
                System.out.println("start - [" + Thread.currentThread().getName() + "]") ;
                select(dataSource);
                System.out.println("end - [" + Thread.currentThread().getName() + "]");
            }
        });
        thread.setName("thread-1");
        thread.start();
    }

    private static void select(HikariDataSource dataSource){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            System.out.println("conn [" + Thread.currentThread().getName() +"]: "+System.identityHashCode(connection));
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select t.*,tbname from lp t");
            int count = 0;
            while (resultSet.next()) {
                TaosLp item = new TaosLp();
                item.setTbname(resultSet.getString("tbname"));
                Date ts = resultSet.getDate("ts");
                item.setTs(DateUtil.format(ts,"yyyy-MM-dd HH:mm:ss"));
                item.setLafp(resultSet.getFloat("lafp"));
                item.setLasp(resultSet.getFloat("lasp"));
                item.setLaip(resultSet.getFloat("laip"));
                item.setLcfp(resultSet.getFloat("lcfp"));
                item.setLcsp(resultSet.getFloat("lcsp"));
                item.setLcip(resultSet.getFloat("lcip"));
                item.setLzfp(resultSet.getFloat("lzfp"));
                item.setLzsp(resultSet.getFloat("lzsp"));
                item.setLzip(resultSet.getFloat("lzip"));
                item.setModel(resultSet.getString("model"));
                item.setEffective(resultSet.getBoolean("effective"));
                item.setMeasureId(resultSet.getLong("measure_id"));
                item.setProductKey(resultSet.getString("product_key"));
                count++;
                System.out.println(item.toString());
            }
            System.out.println("count ["  + Thread.currentThread().getName() + "]: " + count);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                try {
                    System.out.println("close [" + Thread.currentThread().getName() +"]: "+System.identityHashCode(connection));
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
